/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.alarm.overview;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import twaver.AlarmSeverity;
import twaver.AlarmStateStatistics;
import twaver.DataBoxSelectionEvent;
import twaver.DataBoxSelectionListener;
import twaver.Element;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TaskAdapter;
import twaver.TaskScheduler;
import twaver.VisibleFilter;
import demo.DemoPane;

public class AlarmStatisticsDemo extends DemoPane {
	
	MainUI mainUI = new MainUI();
	TDataBox box = mainUI.box;
	AlarmStateStatistics statistics = new AlarmStateStatistics(box);
    Map labels = new HashMap(); 
    JLabel sumLabel = new JLabel();
    
    public AlarmStatisticsDemo() {       	
        this.add(mainUI, BorderLayout.CENTER);
        this.initAlarmStateStatistics();    
        this.box.getAlarmModel().setLimit(100);

        TaskScheduler.getInstance().register(new TaskAdapter() {
            public void run(long clock) {
//            	box.getAlarmModel().setLimit(-1);
//            	long start = System.currentTimeMillis();
//              mainUI.alarmTable.setLazyPublishMode(true);
//            	mainUI.alarmTable.getTableModel().lock();
//            	mainUI.alarmTable.getDataBox().getAlarmModel().startBatch();
            	
//            	for(int i=0; i<1000; i++){
            		mainUI.createAlarm();
//            	}
            	
//            	mainUI.alarmTable.getDataBox().getAlarmModel().endBatch();
//            	mainUI.alarmTable.getTableModel().unlockAndPublish();
//            	System.out.println(System.currentTimeMillis() - start);
            }
            public int getInterval() {
                return 1000;
            }
        });
    }

    private void updateStatistics(){
		Iterator it = labels.entrySet().iterator();
		while(it.hasNext()){
			Entry entry = (Entry)it.next();
			AlarmSeverity severity = (AlarmSeverity)entry.getKey();
			JLabel label = (JLabel)entry.getValue();
			int newCount = statistics.getNewAlarmCount(severity);
			int ackCount = statistics.getAcknowledgedAlarmCount(severity);
			label.setText(newCount + "|" + ackCount);
		}
		sumLabel.setText("sum:" + statistics.getNewAlarmCount() + "|" +  statistics.getAcknowledgedAlarmCount());
    }
    
    private void initAlarmStateStatistics(){
        statistics.setVisibleFilter(new VisibleFilter(){
			public boolean isVisible(Element element) {
				if(box.getSelectionModel().size() == 0){
					return true;
				}else{
					return element.isSelected();
				}
			}
        });
        box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener(){
			public void selectionChanged(DataBoxSelectionEvent e) {
				statistics.reset();
				updateStatistics();
			}
        });
        
        statistics.getPropertyChangeSupport().addPropertyChangeListener(
        		TWaverConst.ALARMSTATE, new PropertyChangeListener(){
					public void propertyChange(PropertyChangeEvent evt) {
						updateStatistics();
					}
        		});
        Collection severities = AlarmSeverity.getAllSeverity();
        JPanel statisticsPane = new JPanel(new GridLayout(1, severities.size()+1));
        statisticsPane.add(sumLabel); 
        sumLabel.setHorizontalAlignment(SwingConstants.CENTER);
        
        Iterator it = severities.iterator();
        while(it.hasNext()){
        	AlarmSeverity severity = (AlarmSeverity)it.next();
        	JLabel label = new JLabel();
        	label.setHorizontalAlignment(SwingConstants.CENTER);
        	statisticsPane.add(label);
        	labels.put(severity, label);
        	label.setOpaque(true);
        	label.setBackground(severity.getColor());
        }
        updateStatistics();
        
        mainUI.network.setToolbar(null);
        mainUI.network.add(statisticsPane, BorderLayout.NORTH);
    }
}